package com.yanqu.road.server.cmd.cross3.xrank.season;

import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.season.BaseSeasonFactory;
import com.yanqu.road.server.logic.season.BaseSeasonRank;
import com.yanqu.road.server.logic.season.BaseSeasonReward;
import com.yanqu.road.server.logic.season.args.ReceiveSeasonRewardResult;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XRANK_GET_SEASON_REWARD_FROM_CROSS, desc = "领取赛季排行奖励 跨服返回排名")
public class GetXRankSeasonRewardFromCrossCmd extends AbstractCommand {

    private int type;
    private int myRank;

    private String reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXRankSeasonRewardFromCrossRespMsg reqMsg = XRankProto.GetXRankSeasonRewardFromCrossRespMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        myRank = reqMsg.getMyRank();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 拿一下配置
        eSeasonRewardType enumType = eSeasonRewardType.getType(type);
        if (enumType == null) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonReward tmpInstance = BaseSeasonFactory.getInstance(enumType, player);
        if (!(tmpInstance instanceof BaseSeasonRank)) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonRank instance = (BaseSeasonRank) tmpInstance;

        // 领取奖励
        ReceiveSeasonRewardResult result = instance.receiveSeasonRankReward(myRank);
        if (result.getRet() != 0) {
            ret = result.getRet();
            return;
        }
        reward = result.getReward();
    }

    @Override
    public void sendData(GamePlayer player) {
        XRankProto.GetXRankSeasonRewardRespMsg.Builder builder = XRankProto.GetXRankSeasonRewardRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setReward(reward);
        }
        player.sendPacket(ClientProtocol.U_XRANK_GET_SEASON_REWARD, builder);
    }
}
